package cn.ococci.dm.Main.View;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;

import cn.ococci.dm.Main.Tools.ThreadTool;
import cn.ococci.dm.Network.Http;
import cn.ococci_dm.R;


/**
 * Created by cyan on 15/9/21.
 * 请求网络提示框
 */
public class WebProgress extends Dialog {

    private int TIME_OUT_SHOW_CANCEL = 4000;

    private static WebProgress instance = null;

    private static Object locker = new Object();

    private Http http;

    private Runnable cancelAction;


    public WebProgress(Context context) {
        this(context, null);
        TIME_OUT_SHOW_CANCEL = 6000;
    }


    public WebProgress(Context context, final Http http) {
        super(context, R.style.CustomProgressDialog);
        setContentView(R.layout.web_progress_dialog);
        getWindow().getAttributes().gravity = Gravity.CENTER;
        findViewById(R.id.loadingIv).startAnimation(AnimationUtils.loadAnimation(context, R.anim.loading_animation));
        setCancelable(false);
        this.http = http;
        final Button cancelBtn = (Button) findViewById(R.id.cancelBtn);
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (http != null)
                    http.cancel();

                if(cancelAction != null){
                    cancelAction.run();
                }

                dismiss();
            }
        });
        ThreadTool.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (instance != null && instance.http == http) {
                    cancelBtn.setVisibility(View.VISIBLE);
                }
            }
        }, TIME_OUT_SHOW_CANCEL);
        show();
    }



    /**
     * 创建一个进度框
     *
     * @return
     */
    public static WebProgress createDialog(Context context, Http http) {
        synchronized (locker) {
            webDismiss();
            instance = new WebProgress(context, http);
        }
        return instance;
    }





    /**
     * 检测对话框是否有效
     * @return
     */
    public static boolean inValidInstance(){

        return instance == null;

    }



    public void setCancelAction(Runnable cancelAction){
        this.cancelAction = cancelAction;
    }

    /**
     * 创建一个普通进度框
     *
     * @return
     */
    public static WebProgress createDialog(Context context) {
        synchronized (locker) {
            webDismiss();
            instance = new WebProgress(context);
        }
        return instance;
    }

    /**
     * 创建一个普通进度框
     *
     * @return
     */
    public static WebProgress createDialog(Context context,Runnable callback) {
        synchronized (locker) {
            webDismiss();
            instance = new WebProgress(context);
            instance.cancelAction = callback;
        }
        return instance;
    }




    public static void webDismiss(){
        synchronized (locker){
            if(instance != null){
                instance.dismiss();
                instance = null;
            }
        }
    }

    public static void setLoadingStr(String str) {
        if (instance != null) {
            TextView tv = (TextView) instance.findViewById(R.id.loadingStrTv);
            tv.setText(str);
        }
    }

    public static void setLoadingStr(int resId) {
        if (instance != null) {
            TextView tv = (TextView) instance.findViewById(R.id.loadingStrTv);
            tv.setText(resId);
        }
    }


}
